package com.ilop.sthome.ui.activity.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.alibaba.sdk.android.openaccount.ui.ui.EmailRegisterActivity;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.ui.activity.mine.AgreementActivity;
import com.ilop.sthome.ui.activity.mine.WebViewActivity;
import com.ilop.sthome.utils.ConfigureUtil;
import com.siterwell.familywellplus.R;

/**
 * @author skygge
 * @date 2019-12-27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：邮箱注册
 */

public class EmailRegisterAliActivity extends EmailRegisterActivity {

    private TextView tv_pid;
    private Button mButton;
    private CheckBox mCheckBox;
    private String privacy_agreement;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        initView();
        initData();
        initListener();
    }

    @Override
    protected String getLayoutName() {
        return "activity_mail_register_ali";
    }

    private void initView() {
        this.mToolBar.setVisibility(View.GONE);
        this.emailCodeInputBox.getSend().setText(getResources().getString(R.string.email_register_page_code_button_text));
        this.emailCodeInputBox.getSend().setTextColor(ContextCompat.getColor(this, R.color.main_color));
        this.emailCodeInputBox.setSendText("get_code");
        tv_pid = findViewById(R.id.tv_pid);
        mCheckBox = findViewById(R.id.register_box);
        mButton = findViewById(R.id.email_empty_view);
    }

    private void initData() {
        privacy_agreement = ConfigureUtil.getPrivacyAgreement(this);
        setMsg(getString(R.string.email_register_page_protocol), tv_pid);
    }

    private void initListener(){
        findViewById(R.id.iv_register_back).setOnClickListener(view -> finish());
        mCheckBox.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b){
                mButton.setVisibility(View.GONE);
                next.setVisibility(View.VISIBLE);
                hideSoftKeyboard();
            }else {
                mButton.setVisibility(View.VISIBLE);
                next.setVisibility(View.GONE);
            }
        });
    }

    private void hideSoftKeyboard(){
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(inputMethodManager != null ) {
            View localView = getCurrentFocus();
            if(localView != null && localView.getWindowToken() != null ) {
                IBinder windowToken = localView.getWindowToken();
                inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
            }
        }
    }

    /**
     *
     * @param content   文字内容
     * @param textView  加载文字的textview
     */

    private void setMsg(String content, TextView textView) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        int i = content.indexOf(getResources().getString(R.string.email_register_page_user_agreement_link));//截取文字开始的下标
        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                //点击后的操作
                Intent intent = new Intent();
                intent.setClass(EmailRegisterAliActivity.this, AgreementActivity.class);
                intent.putExtra("isRegister", true);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(true);      //设置下划线//根据需要添加
            }
        }, i, i +getResources().getString(R.string.email_register_page_user_agreement_link).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int i2 = content.indexOf(getResources().getString(R.string.email_register_page_privacy_link));//截取文字开始的下标
        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent();
                intent.setClass(EmailRegisterAliActivity.this, WebViewActivity.class);
                intent.putExtra(CommonId.KEY_AGREEMENT, privacy_agreement);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(true);      //设置下划线//根据需要添加
            }
        }, i2, i2 +getResources().getString(R.string.email_register_page_privacy_link).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明，否则会一直出现高亮
        textView.setText(builder);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
